<?php
declare (strict_types=1);

namespace app\api\controller;

use app\common\model\User;
use app\common\tool\Result;
use think\exception\ValidateException;
use think\facade\View;
use think\response\Json;

class Register extends Base
{
    public function index()
    {
        return View::fetch();
    }


    /**
     * post: 注册
     * path: save
     * method: save
     * param: nickname {string} 昵称
     * param: mobile {string} 电话
     * param: parent_code {string} 推荐人
     * param: password {string} 登录密码
     * param: password_confirm {string} 确认登录密码
     * param: pay_password {string} 支付密码
     * param: pay_password_confirm {string} 确认支付密码
     */
    public function save(): \think\Response
    {
        $param = $this->request->post('', '', $this->filter);
        try {
            $this->validate($param, 'Register');
        } catch (ValidateException $e) {
            return json(['code' => -1, 'msg' => $e->getError()]);
        }
        $user = User::_save($param, '');
        if ($user['code'] > 0) {
            return Result::success($user['msg']);
        } else {
            return Result::fail($user['msg']);
        }
    }

}
